﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using Tebbee.UI.Client.StatisticSer;
using Tebbee.UI.Client.UtilitiesSer;

namespace Tebbee.UI.Client.Controllers.AdminApi
{
    public class EmailManagerController : ApiController
    {
        private readonly UtilitiesClient utilities = new UtilitiesClient();
        private readonly StatisticSerClient db = new StatisticSerClient();

        /// <summary>
        /// save mail format
        /// </summary>
        /// <param name="mail"></param>
        /// <returns></returns>
        public HttpResponseMessage PostEmailFormat(Mail mail)
        {
            HttpResponseMessage result = null;

            var docfiles = new List<string>();

            var fileName = String.Empty;
            var subjecfile = String.Empty;
            switch (mail.Type)
            {
                case 1:
                    fileName = "Email_DangKyThanhCong.html";
                    subjecfile = "Subject_DangKyThanhCong.html";
                    break;
                case 2:
                    fileName = "Email_MuaHangThanhCong.html";
                    subjecfile = "Subject_MuaHangThanhCong.html";
                    break;
                case 3:
                    fileName = "Email_GiaoHangThanhCong.html";
                    subjecfile = "Subject_GiaoHangThanhCong.html";
                    break;
                default:
                    break;
            }
            var filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Advertise/Email/" + fileName);
            var subjectPath = Path.Combine(HttpRuntime.AppDomainAppPath, "Advertise/Email/" + subjecfile);
            try
            {
                utilities.WriteFile(mail.Body,filePath);
                utilities.WriteFile(mail.Subject, subjectPath);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            docfiles.Add(fileName);


            result = Request.CreateResponse(HttpStatusCode.Created, docfiles);


            return result;
        }

        /// <summary>
        /// get an email template
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Mail getEmailTemplate(int id)
        {
            var mail = new Mail();
            var fileName = String.Empty;
            var subjecfile = String.Empty;
            switch (id)
            {
                case 1:
                    fileName = "Email_DangKyThanhCong.html";
                    subjecfile = "Subject_DangKyThanhCong.html";
                    break;
                case 2:
                    fileName = "Email_MuaHangThanhCong.html";
                    subjecfile = "Subject_MuaHangThanhCong.html";
                    break;
                case 3:
                    fileName = "Email_GiaoHangThanhCong.html";
                    subjecfile = "Subject_GiaoHangThanhCong.html";
                    break;
                default:
                    break;
            }
            var pathBody = Path.Combine(HttpRuntime.AppDomainAppPath, "Advertise/Email/" + fileName);
            var pathSub = Path.Combine(HttpRuntime.AppDomainAppPath, "Advertise/Email/" + subjecfile);
            try
            {
                mail.Body = utilities.ReadFile(pathBody);
                mail.Subject = utilities.ReadFile(pathSub);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            return mail;
        }

        /// <summary>
        /// Broadcast email
        /// </summary>
        /// <param name="mail"></param>
        /// <returns></returns>
        public HttpResponseMessage BroadcastEmail(Mail mail)
        {
            HttpResponseMessage result = null;
            try
            {
                var emails = db.Email_GetForBroadcast();
                Parallel.ForEach(emails, email => {
                    utilities.SendMail(email, mail.Body, mail.Subject);
                });
                result = Request.CreateErrorResponse(HttpStatusCode.OK,"Gửi thành công");
            }
            catch (Exception ex)
            {
                result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
            }
            return result;
        }
    }

    /// <summary>
    /// Get mail format
    /// </summary>
    public class Mail
    {
        public int Type { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
    }
}
